package com.blb.thread;

public class Demo7Join {

    static Thread thread1 = null;
    static Thread thread2 = null;

    public static void main(String[] args) {
        thread1 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println(Thread.currentThread().getName()+"---->" + i);
                if(i == 30){
                    try {
                        //合并线程2，让线程2先执行完
                        thread2.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread1.start();
        thread2 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println(Thread.currentThread().getName()+"---->" + i);
                if(i == 30){
                    //合并线程1，让线程1先执行完
                    try {
                        thread1.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread2.start();
    }
}
